/* 
 * Copyright 2014 Igor Maznitsa (http://www.igormaznitsa.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.igormaznitsa.jhexed.swing.editor.ui.dialogs;

import com.igormaznitsa.jhexed.engine.HexEngine;
import com.igormaznitsa.jhexed.renders.svg.SVGImage;
import com.igormaznitsa.jhexed.swing.editor.Log;
import com.igormaznitsa.jhexed.swing.editor.ui.Utils;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.*;
import javax.swing.*;

public class DialogDocumentOptions extends AbstractDialog {

  private static final long serialVersionUID = 252529293457935862L;


  private final DocumentOptions value;
  private DocumentOptions result;

  public DialogDocumentOptions(java.awt.Frame parent, final DocumentOptions options) {
    super(parent, true);
    this.value = options;

    initComponents();

    load();
  }

  private void load() {
    switch (this.value.getHexOrientation()) {
      case HexEngine.ORIENTATION_HORIZONTAL: {
        this.buttonHorizontal.setSelected(true);
      }
      break;
      case HexEngine.ORIENTATION_VERTICAL: {
        this.buttonVertical.setSelected(true);
      }
      break;
      default:
        throw new Error("Unsupported value");
    }

    this.spinnerLineWidth.setValue(this.value.getLineWidth());
    this.spinnerNumberRows.setValue(this.value.getRows());
    this.spinnerNumberColumns.setValue(this.value.getColumns());

    this.buttonChangeImageFile.setIcon(this.value.setImage(this.value.getImage()));
    this.buttonBorderColor.setIcon(this.value.setColor(this.value.getColor()));
    
    this.textCommentary.setText(this.value.getCommentary());
  }

  private void save() {
    if (this.buttonHorizontal.isSelected()) {
      this.value.setHexOrientation(HexEngine.ORIENTATION_HORIZONTAL);
    }
    else if (this.buttonVertical.isSelected()) {
      this.value.setHexOrientation(HexEngine.ORIENTATION_VERTICAL);
    }
    else {
      throw new Error("Unknown state");
    }
    this.value.setColumns((Integer) this.spinnerNumberColumns.getValue());
    this.value.setRows((Integer) this.spinnerNumberRows.getValue());
    this.value.setLineWidth((Float) this.spinnerLineWidth.getValue());
    
    this.value.setCommentary(this.textCommentary.getText());
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    buttonGroup1 = new javax.swing.ButtonGroup();
    buttonCancel = new javax.swing.JButton();
    buttonOk = new javax.swing.JButton();
    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    spinnerNumberColumns = new javax.swing.JSpinner();
    spinnerNumberRows = new javax.swing.JSpinner();
    jPanel2 = new javax.swing.JPanel();
    buttonHorizontal = new javax.swing.JRadioButton();
    buttonVertical = new javax.swing.JRadioButton();
    buttonBorderColor = new javax.swing.JButton();
    jLabel3 = new javax.swing.JLabel();
    spinnerLineWidth = new javax.swing.JSpinner();
    jPanel3 = new javax.swing.JPanel();
    buttonChangeImageFile = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    textCommentary = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Document options");
    setLocationByPlatform(true);

    buttonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/igormaznitsa/jhexed/swing/editor/icons/cross.png"))); // NOI18N
    buttonCancel.setText("Cancel");
    buttonCancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonCancelActionPerformed(evt);
      }
    });

    buttonOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/igormaznitsa/jhexed/swing/editor/icons/tick.png"))); // NOI18N
    buttonOk.setText("Ok");
    buttonOk.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonOkActionPerformed(evt);
      }
    });

    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cell field metrics"));

    jLabel1.setText("Number of columns:");

    jLabel2.setText("Number of rows:");

    spinnerNumberColumns.setModel(new javax.swing.SpinnerNumberModel(32, 1, 1024, 1));

    spinnerNumberRows.setModel(new javax.swing.SpinnerNumberModel(32, 1, 1024, 1));

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jLabel2)
          .addComponent(jLabel1))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(spinnerNumberRows, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
          .addComponent(spinnerNumberColumns))
        .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(spinnerNumberColumns, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel2)
          .addComponent(spinnerNumberRows, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(0, 20, Short.MAX_VALUE))
    );

    jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Hex view parameters"));

    buttonGroup1.add(buttonHorizontal);
    buttonHorizontal.setText("Horizontal");

    buttonGroup1.add(buttonVertical);
    buttonVertical.setText("Vertical");

    buttonBorderColor.setText("Border color");
    buttonBorderColor.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonBorderColorActionPerformed(evt);
      }
    });

    jLabel3.setText("Line width:");

    spinnerLineWidth.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(1.0f), Float.valueOf(0.2f), Float.valueOf(20.0f), Float.valueOf(0.1f)));

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(jPanel2Layout.createSequentialGroup()
            .addComponent(buttonVertical)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(buttonBorderColor, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
            .addComponent(buttonHorizontal)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel3)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(spinnerLineWidth, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap())
    );
    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(buttonHorizontal)
          .addComponent(spinnerLineWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel3))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(buttonVertical)
          .addComponent(buttonBorderColor))
        .addContainerGap(13, Short.MAX_VALUE))
    );

    jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Background image"));

    buttonChangeImageFile.setText("Change background image");
    buttonChangeImageFile.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonChangeImageFileActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(
      jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel3Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(buttonChangeImageFile, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addContainerGap())
    );
    jPanel3Layout.setVerticalGroup(
      jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel3Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(buttonChangeImageFile)
        .addContainerGap())
    );

    jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Commentary"));

    textCommentary.setColumns(20);
    textCommentary.setRows(5);
    jScrollPane1.setViewportView(textCommentary);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jScrollPane1)
          .addGroup(layout.createSequentialGroup()
            .addGap(0, 0, Short.MAX_VALUE)
            .addComponent(buttonOk)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(buttonCancel))
          .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addContainerGap())
    );

    layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonCancel, buttonOk});

    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(buttonCancel)
          .addComponent(buttonOk))
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void buttonBorderColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBorderColorActionPerformed
    final Color newColor = JColorChooser.showDialog(this, "Select border color", this.value.getColor());
    if (newColor != null) {
      this.buttonBorderColor.setIcon(this.value.setColor(newColor));
    }
  }//GEN-LAST:event_buttonBorderColorActionPerformed

  private void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed
    save();
    this.result = this.value;
    setVisible(false);
  }//GEN-LAST:event_buttonOkActionPerformed

  private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
    setVisible(false);
  }//GEN-LAST:event_buttonCancelActionPerformed

  private void buttonChangeImageFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonChangeImageFileActionPerformed
    final JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(true);
    fileChooser.setFileFilter(Utils.SVG_FILE_FILTER);

    if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
      final File file = fileChooser.getSelectedFile();
      final SVGImage svgImage;
      try {
        svgImage = new SVGImage(file);
        this.buttonChangeImageFile.setIcon(this.value.setImage(svgImage));
      }
      catch (Exception ex) {
        Log.error("Can't load image ["+file+']', ex);
        JOptionPane.showMessageDialog(this, "Can't load the image for error", "Error", JOptionPane.ERROR_MESSAGE);
        return;
      }
    }

  }//GEN-LAST:event_buttonChangeImageFileActionPerformed

  public DocumentOptions getResult() {
    return this.result;
  }


  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton buttonBorderColor;
  private javax.swing.JButton buttonCancel;
  private javax.swing.JButton buttonChangeImageFile;
  private javax.swing.ButtonGroup buttonGroup1;
  private javax.swing.JRadioButton buttonHorizontal;
  private javax.swing.JButton buttonOk;
  private javax.swing.JRadioButton buttonVertical;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JPanel jPanel3;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JSpinner spinnerLineWidth;
  private javax.swing.JSpinner spinnerNumberColumns;
  private javax.swing.JSpinner spinnerNumberRows;
  private javax.swing.JTextArea textCommentary;
  // End of variables declaration//GEN-END:variables

  @Override
  public void processEscape(final ActionEvent e) {
    buttonCancelActionPerformed(e);
  }
}
